<template>
    <div>
        <template v-if="login">
        <div class="layout" :class="{'layout-hide-text': spanLeft < 5}">
            <Row type="flex">
                <Col :span="spanLeft" class="layout-menu-left">
                    <Menu active-name="1" theme="dark" width="auto">
                        <div class="layout-logo-left"></div>
                        <MenuItem name="1">
                            <Icon type="ios-navigate" :size="iconSize"></Icon>
                            <router-link to="/" tag="span" class="layout-text">主页</router-link>
                            <!-- <span class="layout-text">选项 1</span> -->
                        </MenuItem>
                        <MenuItem name="2">
                            <Icon type="ios-keypad" :size="iconSize"></Icon>
                            <router-link to="/user" tag="span" class="layout-text">用户</router-link>
                            <!-- <span class="layout-text">选项 2</span> -->
                        </MenuItem>
                        <MenuItem name="3">
                            <Icon type="ios-analytics" :size="iconSize"></Icon>
                            <router-link to="/cate" tag="span" class="layout-text">分类</router-link>
                            <!-- <span class="layout-text">选项 3</span> -->
                        </MenuItem>
                        <MenuItem name="4">
                            <Icon type="ios-analytics" :size="iconSize"></Icon>
                            <router-link to="/news" tag="span" class="layout-text">新闻</router-link>
                            <!-- <span class="layout-text">选项 3</span> -->
                        </MenuItem>
                        <MenuItem name="5">
                            <Icon type="ios-analytics" :size="iconSize"></Icon>
                            <router-link to="/comment" tag="span" class="layout-text">评论</router-link>
                            <!-- <span class="layout-text">选项 3</span> -->
                        </MenuItem>
                        <MenuItem name="6">
                            <Icon type="ios-analytics" :size="iconSize"></Icon>
                            <router-link to="/upload" tag="span" class="layout-text">上传</router-link>
                            <!-- <span class="layout-text">选项 3</span> -->
                        </MenuItem>
                        <MenuItem name="7">
                            <Icon type="ios-analytics" :size="iconSize"></Icon>
                            <span class="layout-text" @click="Quit">注销</span>
                        </MenuItem>
                    </Menu>
                </Col>
                <Col :span="spanRight">
                    <div class="layout-header">
                        <Button type="text" @click="toggleClick">
                            <Icon type="navicon" size="32"></Icon>
                        </Button>
                    </div>
                    
                    <div class="layout-content">
                    
                        <router-view></router-view>

                    </div>
                    <div class="layout-copy">
                        2011-2016 &copy; TalkingData
                    </div>
                </Col>
            </Row>
        </div>
        </template>
        <template v-else>
            <Form id="login" ref="formInline" :model="formInline" :rules="ruleInline" inline>
                <h2 style="text-align:center; text-indent:-4px;">用户登陆</h2><br>
                <FormItem prop="name">
                    <Input type="text" v-model="formInline.name" placeholder="Username">
                        <Icon type="ios-person-outline" slot="prepend"></Icon>
                    </Input>
                </FormItem>
                <FormItem prop="password">
                    <Input type="password" v-model="formInline.password" placeholder="Password">
                        <Icon type="ios-locked-outline" slot="prepend"></Icon>
                    </Input>
                </FormItem>
                <FormItem>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <Button  type="primary" @click="handleSubmit('formInline')">登录</Button>
                </FormItem>
            </Form>

        </template>
    </div>
</template>
<script>
//mapGetters 都放在computed
//mapactions 都放在methods 
import sha256 from 'crypto-js/sha256';
import {mapGetters ,mapActions} from 'vuex';
    export default {
        data(){
            return{
                spanLeft: 5,
                spanRight: 19,
                formInline: {
                    name: '',
                    password: ''
                },
                ruleInline: {
                    name: [
                        { required: true, message: '请填写用户名', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请填写密码', trigger: 'blur' },
                        { type: 'string', min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
                    ]
                }
            }
        },
        computed: {
            ...mapGetters(['login']),
            iconSize () {
                return this.spanLeft === 5 ? 14 : 24;
            }
        },
        methods: {
            ...mapActions(['checkLogin']),
            toggleClick () {
                if (this.spanLeft === 5) {
                    this.spanLeft = 2;
                    this.spanRight = 22;
                } else {
                    this.spanLeft = 5;
                    this.spanRight = 19;
                }
            },
            handleSubmit(name) {
                this.formInline.password=sha256(this.formInline.password).toString();
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        
                        this.$http({
                            url:'http://localhost:3000/users/login',
                            data:this.formInline,
                            method:'post'
                        }).then(res=>{
                            if(res.data && res.data.name.trim().length>0){
                                this.checkLogin(true);
                                console.log(sha256(res.data.password));
                                this.formInline.password='';
                                this.formInline.name='';
                            }
                           
                        })
                    } else {
                        this.$Message.error('表单验证失败!');
                        
                    }
                })
            },
            Quit(){
               this.checkLogin(false);
            }
        }
    }
</script>
<style scoped>

    .layout{
        border: 1px solid #d7dde4;
        background: #f5f7f9;
        position: relative;
        border-radius: 4px;
        overflow: hidden;
    }
    .layout-breadcrumb{
        padding: 10px 15px 0;
    }
    .layout-content{
        min-height: 590px;
        margin: 0px;
        overflow: hidden;
        background: #fff;
        border-radius: 4px;
    }
    
    .layout-copy{
        text-align: center;
        padding: 10px 0 20px;
        color: #9ea7b4;
    }
    .layout-menu-left{
        background: #464c5b;
    }
    .layout-header{
        height: 60px;
        background: #fff;
        box-shadow: 0 1px 1px rgba(0,0,0,.1);
    }
    .layout-logo-left{
        width: 90%;
        height: 30px;
        background: #5b6270;
        border-radius: 3px;
        margin: 15px auto;
    }
    .layout-ceiling-main a{
        color: #9ba7b5;
    }
    .layout-hide-text .layout-text{
        display: none;
    }
    .ivu-col{
        transition: width .2s ease-in-out;
    }
   #login{
       width:200px;
       margin:230px auto;
   }
</style>